import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Ga.Implementations.Points.PointsGA;

import javax.swing.*;

public class TestPointsGA extends JFrame 
{
	PointsGA pointsGA;
	public static void main(String[] args)
	{
		TestPointsGA tpoints = new TestPointsGA("Frame");
	}
	
	public TestPointsGA(String name)
	{
		super(name);
		this.pointsGA = new PointsGA();
		this.pointsGA.initialize();
		this.pointsGA.show();
		
		for(int i = 0; i < 100; i++)
			this.pointsGA.evaluatePopulation();
		
		System.out.println(".........................");
		this.pointsGA.show();		

		this.createComponents();
	}	
		
	public void createComponents()
	{
		
		this.setForeground(Color.BLACK);

		//2. Optional: What happens when the frame closes?
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Add components
		JButton btnUpdate = new JButton("Update");
		btnUpdate.setSize(100, 50);
		btnUpdate.setLocation(0, 100);
		btnUpdate.addActionListener(
			    new ActionListener() {
			        public void actionPerformed(ActionEvent e) {
			            JFrame frame = (JFrame)(((JButton)e.getSource()).getParent().getParent().getParent().getParent());
			            frame.repaint();
			        }
			    }
			);
		
		this.getContentPane().add(btnUpdate);
		this.getContentPane().add(new PointsDrawer(this.pointsGA));
		//4. Size the frame.
		this.setSize(100, 150);
		
		//5. Show it.
		this.setVisible(true);		
	}

	
	public class PointsDrawer extends JComponent
	{
		PointsGA pointsGA;

		public PointsDrawer(PointsGA pGA)
		{
			this.pointsGA = pGA;
		}
		public void paintComponent(Graphics g)
		{
			g.setColor(Color.black);
			
			for (Ga.Implementations.Points.Point p: this.pointsGA.getPoints())
			{
				g.drawLine(p.getX(), p.getY(), p.getX(), p.getY());
			}
		}
	}	
}
